package xyz.lwm.lazycat.transaction.policy;

import xyz.lwm.lazycat.transaction.execute.TranDefinition;
import xyz.lwm.lazycat.transaction.execute.TransactionException;
import xyz.lwm.lazycat.transaction.execute.TransactionManager;

/**
 * 以非事务的方式运行，若存在当前事务，则抛出异常
 *
 * @author lwm
 * @see xyz.lwm.lazycat.transaction.execute.Propagation#NEVER
 */
class NeverPolicy extends AbstractPolicy {

    public NeverPolicy(TranDefinition tranDefinition) {
        super(tranDefinition);
    }

    @Override
    public <T> T execute(CheckedSupplier<T> supplier) throws Throwable {
        if (TransactionManager.current().isOpen()) {
            throw new TransactionException("Transaction is not allowed");
        }
        return supplier.get();
    }

}
